package com.chia.multienty.workflow.service.impl;

import com.chia.multienty.core.parameter.workflow.FlowableGroupAddParameter;
import com.chia.multienty.core.parameter.workflow.FlowableGroupListGetParameter;
import com.chia.multienty.core.parameter.workflow.FlowableGroupDeleteParameter;
import com.chia.multienty.workflow.service.FlowableGroupService;
import lombok.RequiredArgsConstructor;
import org.flowable.engine.IdentityService;
import org.flowable.idm.api.Group;
import org.flowable.idm.engine.impl.persistence.entity.GroupEntity;
import org.flowable.idm.engine.impl.persistence.entity.GroupEntityImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class FlowableGroupServiceImpl implements FlowableGroupService {
    private final IdentityService identityService;

    @Override
    public void add(FlowableGroupAddParameter parameter) {
        GroupEntity group = new GroupEntityImpl();
        group.setName(parameter.getName());
        group.setId(parameter.getId());
        identityService.saveGroup(group);
    }
    @Override
    public void delete(FlowableGroupDeleteParameter parameter) {
        identityService.deleteGroup(parameter.getGroupId());
    }

    @Override
    public List<Group> list(FlowableGroupListGetParameter parameter) {
        List<Group> groups = identityService.createGroupQuery()
                .orderByGroupId()
                .desc()
                .listPage(parameter.getCurrentPage(), parameter.getPageSize());
        return groups;
    }
}
